using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Text;
using System.Data.Common;
using System.Globalization;
using System.Collections.Generic;
using System.Xml.XPath;
using Instedd.Labs.Forms.Library;
using System.Transactions;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string content = this.Request.ContentEncoding.GetString(this.Request.BinaryRead(this.Request.ContentLength));

            XPathDocument surveyDocument = SurveyManager.GetInstance().CreateSurvey(content);

            //TODO: Improve the performance of the xpath execution compiling the expressions.
            XPathNavigator surveyNode = surveyDocument.CreateNavigator().SelectSingleNode("Survey");

            if (SurveyManager.GetInstance().Update(surveyNode, true) == 0)
            {
                SurveyManager.GetInstance().Save(surveyNode);
            }
        }
        catch (InvalidFormVersionException ex)
        {
            Response.Output.Write("You're using an outdated client. Please download the latest version from http://bioforms.clariusconsulting.net");
            Response.StatusCode = 888;
            Response.End();
        }
        catch (Exception ex)
        {
            String msg = String.Format("{0}, {1}", ex.Message, ex.StackTrace != null ? ex.StackTrace : String.Empty);
            Response.Output.Write(String.Format("{0}, {1}", ex.Message, ex.StackTrace != null ? ex.StackTrace : String.Empty));
            Response.StatusCode = 500;
            Response.End();
        }

        Response.StatusCode = 200;
        Response.End();
    }
}